package com.zpb.framework.utils;

import com.zpb.framework.domain.entity.UserInfo;
import com.zpb.framework.mapper.RoleMapper;
import com.zpb.framework.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Author: zhaopengbo
 * @Date: 2025/9/13
 */
@Component
public class SecurityUtils {
    private static RoleMapper roleMapper;
    @Autowired
    public void setRoleMapper(RoleMapper roleMapper) {
        SecurityUtils.roleMapper = roleMapper; // 关键：为静态字段赋值
    }
    public static Authentication getAuthentication(){
        return SecurityContextHolder.getContext().getAuthentication();
    }
    public static UserInfo getUserInfo(){
        return (UserInfo) getAuthentication().getPrincipal();
    }
    public static Long getCurrentId(){
        return getUserInfo().getUser().getId();
    }
    public static boolean isAdmin(){
        Long roleId = roleMapper.getRoleIdByUserId(getCurrentId());
        if (roleId == null) {
            return false;
        }
        if(roleId == 1L){
            return true;
        }
        return false;
    }
}
